package cn.bdqn.service.impl;

import cn.bdqn.bean.result.Page;
import cn.bdqn.domain.Damage;
import cn.bdqn.mapper.DamageMapper;
import cn.bdqn.service.DamageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(value = "damageService")
public class DamageServiceImpl implements DamageService {

    @Autowired
    private DamageMapper damageMapper;

    @Override
    public Page<Damage> queryAll(Integer startNum) {
        Page page=new Page();

        Integer pageSize = page.getPageSize();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != 0 ? startNum:1,pageSize);
        List<Damage> damageList=damageMapper.selectAll();

        //分页
        PageInfo<Damage> pageInfo=new PageInfo<Damage>(damageList);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    @Override
    public Page<Damage> queryByType(Integer type, Integer startNum) {
        Page page=new Page();

        Integer pageSize = page.getPageSize();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != 0 ? startNum:1,pageSize);
        List<Damage> damageList=damageMapper.selectByType(type);

        //分页
        PageInfo<Damage> pageInfo=new PageInfo<Damage>(damageList);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    @Override
    public Page<Damage> queryByName(String damageName, Integer startNum) {
        Page page=new Page();

        Integer pageSize = page.getPageSize();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != 0 ? startNum:1,pageSize);
        List<Damage> damageList=damageMapper.selectByName(damageName);

        //分页
        PageInfo<Damage> pageInfo=new PageInfo<Damage>(damageList);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    //根据申请编号查询
    @Override
    public Page<Damage> queryByApplyNo(String applyNo, Integer startNum) {
        Page page=new Page();

        Integer pageSize = page.getPageSize();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != 0 ? startNum:1,pageSize);
        List<Damage> damageList=damageMapper.selectByApplyNo(applyNo);

        //分页
        PageInfo<Damage> pageInfo=new PageInfo<Damage>(damageList);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    @Override
    public Page<Damage> querySelective(Damage damage, Integer startNum) {
        Page page=new Page();

        Integer pageSize = page.getPageSize();
        //判断当前是第几页,如果startNum为空
        PageHelper.startPage(startNum != 0 ? startNum:1,pageSize);
        List<Damage> damageList=damageMapper.selectSelective(damage);

        //分页
        PageInfo<Damage> pageInfo=new PageInfo<Damage>(damageList);
        //获取分页之后的数据
        page.setMenuList(pageInfo.getList());
        //总记录数
        page.setTotalRecord((int)pageInfo.getTotal());
        System.out.println(page);
        //返回分页对象
        return  page;
    }

    @Override
    public void addDamage(Damage damage) {
        damageMapper.insertSelective(damage);
    }

    @Override
    public void changeById(Damage damage) {
        damageMapper.updateByPrimaryKeySelective(damage);
    }

    @Override
    public void removeById(Integer id) {
        damageMapper.deleteByPrimaryKey(id);
    }
}
